<?php
class AssessmentAction extends Action{
    public function index(){
        header("Content-Type:text/html; charset=utf-8");
        $this->display();
    }
public function insert(){
    header("Content-Type:text/html; charset=utf-8");
    
        //echo $_POST["age"];
        //正则表达式
        //...
        //转换类型
    	$age=(int)($_POST["age"]);
    	$height=(float)($_POST["height"]);
    	$weight=(float)($_POST["weight"]);
    	$waistline=(float)($_POST["waistline"]);
    	$sex=(string)($_POST["sex"]);
    	
    	//功能函数
    	$level=0;//层次
    	if ($age>=18)
    	{
   			$BMI=round($weight/$height/$height*10000,2);
        	if($sex=="男")
        	{
        		if($BMI<18.5)
        		{
        			$risk="正常";
        			$level=1;
        		}
        		elseif ($BMI>=18.5and$BMI<24)
        		{
        			if($waistline<85)
        			{
        				$risk="正常";
        				$level=2;
        			}
        			elseif($waistline>=85and$waistline<95)
        			{
        				$risk="增加";
        				$level=2;
        			}
        			else 
        			{
        				$risk="高";
        				$level=3;
        			}
        		}
        		elseif ($BMI>=24and$BMI<28)
        		{
        			if($waistline<85)
        			{
        				$risk="增加";
        				$level=4;
        			}
        			elseif($waistline>=85and$waistline<95)
        			{
        				$risk="高";
        				$level=4;
        			}
        			else 
        			{
        				$risk="极高";
        				$level=5;
        			}
        		}
        		else 
        		{
        			if($waistline<85)
        			{
        				$risk="高";
        				$level=6;
        			}
        			elseif($waistline>=85and$waistline<95)
        			{
        				$risk="极高";
        				$level=6;
        			}
        			else 
        			{
        				$risk="极高";
        				$level=6;
        			}
        		}
        	}
        	elseif ($sex=="女")
        	{
        	if($BMI<18.5)
        		{
        			$risk="正常";
        			$level=1;
        		}
        		elseif ($BMI>=18.5and$BMI<24)
        		{
        			if($waistline<80)
        			{
        				$risk="正常";
        				$level=2;
        			}
        			elseif($waistline>=80and$waistline<90)
        			{
        				$risk="增加";
        				$level=2;
        			}
        			else 
        			{
        				$risk="高";
        				$level=3;
        			}
        		}
        		elseif ($BMI>=24and$BMI<28)
        		{
        			if($waistline<80)
        			{
        				$risk="增加";
        				$level=4;
        			}
        			elseif($waistline>=80and$waistline<90)
        			{
        				$risk="高";
        				$level=4;
        			}
        			else 
        			{
        				$risk="极高";
        				$level=5;
        			}
        		}
        		else 
        		{
        			if($waistline<80)
        			{
        				$risk="高";
        				$level=6;
        			}
        			elseif($waistline>=80and$waistline<90)
        			{
        				$risk="极高";
        				$level=6;
        			}
        			else 
        			{
        				$risk="极高";
        				$level=6;
        			}
        		}
        	}
    	}
    	//不满18周岁的青少年
    	else
    	{
    	
    	}
    	switch ($level)
    	{
    		case 1:
    			$suggestion="增肌";
    			break;
    		case 2:
    			$suggestion="维持现有体重";
    			break;
    		case 3:
    			$suggestion="减腹部脂肪";
    			break;
    		case 4:
    			$suggestion="开始减肥";
    			break;
    		case 5:
    			$suggestion="减肥和减腹部脂肪";
    			break;
    		case 6:
    			$suggestion="减肥并筛查相关疾病";
    			break;
    		default:
    			break;
    	}
    	//用session存储数据
    	session('myheight',$height);
    	session('myweight',$weight);
    	session('mywaistline',$waistline);
    	session('myage',$age);
    	session('mysex',$sex);
    	session('mylevel',$level);
    	session('myBMI',$BMI);
    	//传值
        $this->assign( "BMI", $BMI );
        $this->assign( "waistline",$waistline  );
        $this->assign( "risk", $risk );
        $this->assign( "suggestion",$suggestion  );
        	
        $this->display();
}
	public function insert2(){
        header("Content-Type:text/html; charset=utf-8");
        //session取数据
        $weight=session('myweight');
        $this->assign( "weight", $weight );
        $this->display();
    }
	public function insert3(){
        header("Content-Type:text/html; charset=utf-8");
        //session取数据
        $height=session('myheight');
        $weightl=18.5*$height*$height/10000;
        $weightu=23.9*$height*$height/10000;
        
        $skeleton=(float)($_POST["skeleton"]);
        $weightl=round($weightl*$skeleton,1);
        $weightu=round($weightu*$skeleton,1);
        
        $this->assign( "weightl", $weightl );
        $this->assign( "weightu", $weightu );
        $this->display();
    }
	public function insert4(){
        header("Content-Type:text/html; charset=utf-8");
        //目标体重判断！
        
        //session取数据
        $weight=session('myweight');
        $targetweight=(float)($_POST["targetweight"]);
        $week=ceil(($weight-$targetweight)/3.6);
        $kcal=ceil(($weight-$targetweight)*7700);
        //用session存储数据
        session('mytargetweight',$targetweight);
        //传值
        $this->assign( "week", $week );
        $this->assign( "kcal", $kcal );
        $this->display();
    }
	public function insert5(){
        header("Content-Type:text/html; charset=utf-8");
        //目标周数判读！
        
        $targetweek=(float)($_POST["targetweek"]);
        $weightlose=$targetweek*0.6;
        //用session存储数据
        session('mytargetweek',$targetweek);
        //传值
        $this->assign( "weightlose", $weightlose );
        $this->display();
    }
	public function insert6(){
        header("Content-Type:text/html; charset=utf-8");
        
        $this->display();
    }
	public function insert7(){
        header("Content-Type:text/html; charset=utf-8");
        $activitylevel=(float)($_POST["activitylevel"]);
        //session取数据
        $height=session('myheight');
        $weight=session('myweight');
        $age=session('myage');
        $sex=session('mysex');
        $level=session('mylevel');
        $BMI=session('myBMI');
        //功能函数
		switch ($level)
    	{
    		case 1:
    			$A=0;$B=-200;
    			$consumptionl=150;$consumptionu=300;
    			break;
    		case 2:
    			$A=200;$B=0;
    			$consumptionl=180;$consumptionu=400;
    			break;
    		case 3:
    			$A=500;$B=100;
    			$consumptionl=200;$consumptionu=400;
    			break;
    		case 4:
    			$A=700;$B=400;
    			$consumptionl=230;$consumptionu=420;
    			break;
    		case 5:
    			$A=800;$B=350;
    			$consumptionl=250;$consumptionu=450;
    			break;
    		case 6:
    			$A=600;$B=300;
    			$consumptionl=200;$consumptionu=450;
    			break;
    		default:
    			break;
    	}
    	if ($sex=="男")
    	{
    		$BMR=66.473+(13.751*$weight)+(5.0033*$height)-(6.755*$age);
    	}
    	elseif ($sex=="女")
    	{
    		$BMR=655.0955+(9.463*$weight)+(1.8496*$height)-(4.6756*$age);
    	}
    	$intakel=round($BMR*$activitylevel-$A,2);
    	$intakeu=round($BMR*$activitylevel-$B,2);
    	
    	$HRmax=220-age;
    	//确定R
    	$Rl=0;$Ru=0;
		switch ($activitylevel)
    	{
    		case 1.15:
    			$Rl=0.57;$Ru=0.67;
    			break;
    		case 1.35:
    			$Rl=0.64;$Ru=0.74;
    			break;
    		case 1.45:
    			$Rl=0.65;$Ru=0.75;
    			break;
    		case 1.5:
    			$Rl=0.65;$Ru=0.8;
    			break;
    		case 1.8:
    			$Rl=0.68;$Ru=0.85;
    			break;
    		default:
    			break;
    	}
    	$Bheart=0;
    	if ($BMI>=28)
    	{
    		$Bheart=0.9;
    	}
    	elseif ($BMI<28)
    	{
    		$Bheart=1;
    	}
    	$heartratel=$HRmax*$Rl*$Bheart;
    	$heartrateu=$HRmax*$Ru*$Bheart;
        //用session存储数据
        session('myactivitylevel',$activitylevel);
        //传值
        $this->assign( "intakel", $intakel );
        $this->assign( "intakeu", $intakeu );
        $this->assign( "consumptionl", $consumptionl );
        $this->assign( "consumptionu", $consumptionu );
        $this->assign( "heartratel", $heartratel );
        $this->assign( "heartrateu", $heartrateu );
        $this->display();
    }
}
?>